package com.example.service;

import com.example.entity.Dormitory;
import com.example.entity.Repair;
import com.example.entity.Student;
import com.example.mapper.DormitoryMapper;
import com.example.mapper.RepairMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepairService {
    @Autowired
    private RepairMapper repairMapper;
    private boolean existsInDb(Repair repair) {
        List<Repair> repairsList = repairMapper.findAllBy(repair.getRepair_num());
        return !repairsList.isEmpty();
    }

    public int save(Repair repair){
        if(existsInDb(repair) ) {
            return repairMapper.update(repair);
        }else {
            return repairMapper.insert(repair);
        }
    }

    public Map<String, Object> saveRepair(Repair repair) {
        Map<String, Object> result = new HashMap<>();

        Integer saveResult = save(repair);

        if (saveResult != null) {
            result.put("code", 200);
            result.put("message", "保存成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "保存失败");
            result.put("data", null);
        }

        return result;
    }

    public Map<String, Object> deleteByRepairNum(String repair_num) {
        Map<String, Object> result = new HashMap<>();

        Integer deleteResult = repairMapper.deleteBynum(repair_num);

        if (deleteResult != null) {
            result.put("code", 200);
            result.put("message", "删除成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "删除失败");
            result.put("data", null);
        }

        return result;
    }
}
